import pandas as pd
from math import sqrt
from conf import *



def load_graph(data):   # len=25
    # 通过标号获取图形信息（或反向获取）
    example = data[0]
    if example in LOCAL_GRAPH.keys():   # from alias to graph
        rt = []
        num = 1
        for i in data:
            rt.append([num, LOCAL_GRAPH[i] / 10, LOCAL_GRAPH_CORE[i][0] / 10, LOCAL_GRAPH_CORE[i][1] / 10])
            num += 1
    elif example in LOCAL_GRAPH.values():   # from graph to alias
        rt = []
        # <<反向获取标号
    else:
        print(example)
        raise ValueError
    return rt


def get_core(graph):  # 外心
    if len(graph) == 3: # <<<推导
        x1,x2,x3 = graph[0,0], graph[1,0], graph[2,0]
        y1,y2,y3 = graph[0,1], graph[1,1], graph[2,1]
        k1 = (x1 * x1 - x2 * x2 + y1 * y1 - y2 * y2)
        k2 = (x1 * x1 - x3 * x3 + y1 * y1 - y3 * y3)
        k3 = (y1 - y2) * (x1 - x3) - (y1 - y3) * (x1 - x2)
        x = -(k1 * (y1 - y3) - k2 * (y1 - y2)) / k3 / 2
        y = (k1 * (x1 - x3) - k2 * (x1 - x2)) / k3 / 2
    else:
        x1,x3, = graph[0,0], graph[2,0]
        y1,y3, = graph[0,1], graph[2,1]
        x = (x1 + x3) / 2
        y = (y1 + y3) / 2
    return x,y


class data_bridge():
    def __init__(self):
        self.root_addr = '' # 读取数据所在的文件夹
        self.output_name = OUTPUT_NAME   # 输出数据的名字

    def load_from_MB(self, csv_adress): # 返回获取原始数据：[[1, [[0, 0], [40, 0], [40, 40], [0, 40]], [20, 20], [20, 20]], [[2..
        try:
            fd = open(csv_adress, 'r')
            k = fd.read()
            print('MB:', k)
            self.root_addr = csv_adress.split('/')
            self.root_addr.pop()
            self.root_addr = '/'.join(self.root_addr)
            dumped = [int(i) for i in k.split(',')[:25]]    # 切分
        except Exception as e:
            raise e
        hape = load_graph(dumped)  # 准备运算的数据: [[1, [[0, 0], [40, 0], [40, 40], [0, 40]], [20, 20], [20, 20]], [[2..
        return hape

    def post_to_MB(self, data, address=None):#
        if not address:
            address = self.root_addr + '/' + self.output_name
        else:
            address = address
        # dumped_data = data
        dumped_data = [i[0] for i in data]  # 0-24：序号
        dumped_data.extend([i[1][0] for i in data])   # 25-49：重心X
        dumped_data.extend([i[1][1] for i in data])   # 50-74：重心Y
        dumped_data.extend([i[2] for i in data])   # 75-99：旋转角度
        dumped_data.extend([100, 333])  # 物料个数×4，校验码333
        df = pd.DataFrame(dumped_data)
        df.to_csv(address, index=False, header=False)
        print('保存数据：',dumped_data)
        print(len(dumped_data))
        print(address)

    def expand_graphs(self, load_data):
        # data from MB
        graphs = [i[1] for i in load_data]
        for graph in graphs:
            core = get_core(graph)
            counter = 0
            for point in graph:
                k = point - core
                r = sqrt(k[0]**2 + k[1]**2) # 外接圆半径
                cofficient = EXPAND_DISTANCE / r
                graph[counter] = [k[0] * cofficient, k[1] * cofficient] + point
                counter += 1
        for i in range(len(load_data)): # 还原返回
            load_data[i][1] = graphs[i]
        return load_data


data_bridge = data_bridge()

if __name__ == '__main__':
    origin_data = data_bridge.load_from_MB('/home/suu/Desktop/input.csv')
    print(origin_data)
    k = data_bridge.expand_graphs(origin_data)
    print(k)
    data = [23, 5, 24, 17, 11, 3, 21, 16, 2, 25, 14, 6, 1, 22, 18, 15, 9, 20, 10, 8, 19, 13, 7, 4, 12, 2.0, 6.32, 9.64, 11.788556270541642, 14.437112541083284, 17.719955253557902, 1.0, 4.6264911064067356, 15.5, 4.25, 8.532842712474618, 13.0, 17.28284271247462, 8.73426065802772, 9.814842763553106, 9.73248568555055, 10.856596623027183, 13.762834332820663, 10.19059488886063, 9.855361599920496, 14.359575092850553, 18.59924761543533, 23.029291991850023, 13.185375517745559, 29.696231680661406, 1.5, 1.5, 2.5, 2.5, 1.5, 1.5, 6.25, 4.25, 5.0, 7.5, 7.5, 8.75, 8.75, 13.261727938707965, 11.689308437425748, 10.66858625568431, 14.349880905090284, -1.558396632594576, 19.051099655778245, 8.753272853152689, 19.819189811118953, 19.208612409801837, 12.10725114244837, 15.408617001860257, 22.50281070950738, 0, 0, 90, 90, 0, 0, 90, 0, 0, 0, 0, 0, 0, -21, -32, -19, 165, 104, 33, -118, -162, -38, 13, 118, -5, 100, 333]
    data_bridge.post_to_MB(data)
